/*
 * GTranslation.java
 * Created on 21 juin 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.misc;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.hockeo.vhbase.data.dao.IGEntity;

/**
 * Represents a translated localised representation of some codified text.
 *
 * @version $Id$
 * @author janjanke
 */
@Entity
@Table( name = "m_translation" )
public class GTranslation implements IGEntity
{
  @Id
  @Column( name = "id" )
  @GeneratedValue( strategy = GenerationType.AUTO )
  private int       d_nId;

  @Column( name = "code" )
  private String    d_strCode;

  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "lang" )
  private GLanguage d_language;

  @Column( name = "translation" )
  private String    d_strTranslatedText;

  /** Default no argument constructor. */
  protected GTranslation()
  {}

  public final String getCode()
  {
    return d_strCode;
  }

  public final GLanguage getLanguage()
  {
    return d_language;
  }

  public final String getTranslatedText()
  {
    return d_strTranslatedText;
  }

  private int getId()
  {
    return d_nId;
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GTranslation.class;
  }

  public boolean instanceOf( Class<? extends IGEntity> classOther )
  {
    return classOther.isAssignableFrom( getGEntityClass() );
  }

  /*
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals( Object obj )
  {
    try
    {
      return d_nId == ( (GTranslation) obj ).getId();
    }
    catch ( Exception ex ) // especially ClassCastException(s) may happen
    {
      return false;
    }
  }

  /*
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return d_nId;
  }

  /*
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return d_strCode + " = " + d_strTranslatedText + " (in language: " + d_language.getId() + ")";
  }
}
